import { handlerGobackClick, hint } from "../../../utils/util";
import User from "../../../models/user";
const user = new User();
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    password: "",
  },
  onLoad(options) {
    this.setData({
      phone: options.phone,
    });
  },
  handlerGobackClick,
  pwLogin() {
    if (!this.data.password) {
      hint("请输入密码");
      return;
    }
    let { avatarUrl, nickName } = getApp().globalData.userInfo;
    //绑定微信，并设置密码
    wx.login({
      success: (res) => {
        if (res.code) {
          user
            .wxBinding(
              res.code,
              this.data.phone,
              avatarUrl,
              nickName,
              this.data.password
            )
            .then((res) => {
              let status = res.result.registerStatus;
              if (status === 4) {
                wx.login({
                  success: (res) => {
                    user
                      .wxLogin(res.code)
                      .then((res) => {
                        let registerStatus = res.result.registerStatus;
                        if (registerStatus === 4) {
                          const { user, token } = res.messageBody;
                          wx.setStorageSync("user", user);
                          wx.setStorageSync("token", token);
                          wx.switchTab({
                            url: "/pages/home/index",
                          });
                        }
                      })
                      .catch((err) => {
                        console.log(err);
                      });
                  },
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        }
      },
    });
  },
  inputPassWord(e) {
    this.setData({
      password: e.detail.value,
    });
  },
});
